// ActionScript file
import Components.TitleWindow.twSaveService;

import flash.events.KeyboardEvent;

import mx.controls.Alert;
import mx.events.CloseEvent;
import mx.events.FlexEvent;
import mx.events.ListEvent;
import mx.managers.PopUpManager;

import org.flexlancer.Logger.Logger;
import org.flexlancer.databases.dbServices;

[Bindable]
private var servicesData:Array = new Array();
private var completeServicesData:Array = new Array();


private function init():void{
	try{
		
		this.completeServicesData = dbServices.List();
		this.dgServices.addEventListener(KeyboardEvent.KEY_DOWN, OnKeyDown);
		this.dgServices.addEventListener(ListEvent.ITEM_DOUBLE_CLICK,OndgServices_ItemDoubleClicked);
		this.ShowFilterData();
	}catch(err:Error){
		Logger.LogE(err);
	}
}

private function OndgServices_ItemDoubleClicked(evt:ListEvent):void{
	try{
		this.PerformEditService();
	}catch(err:Error){
		Logger.LogE(err);
	}
}
private function PerformEditService():void{
	try{
		if ( this.dgServices.selectedItem != null ){
			var editService:twSaveService = PopUpManager.createPopUp(this,twSaveService,true) as twSaveService;
			editService.addEventListener(FlexEvent.REMOVE,OnCloseService);
			editService.EditObject = this.dgServices.selectedItem;
			PopUpManager.centerPopUp(editService);
		}else{
			Alert.show("Please select any service for edit","Information");
		}
	}catch(err:Error){
		Logger.LogE(err);
	}
}
private function PerformDeleteService():void{
	try{
		if ( this.dgServices.selectedItem != null ){
			Alert.show("Are you sure to delete \""+this.dgServices.selectedItem.servicename+"\" service?",
			"Delete Confirmation",
			Alert.YES|Alert.NO,
			this,
			this.OnDeleteConfirmation);
		}else{
			Alert.show("Please select any service for delete","Information");
		}
	}catch( err:Error){
		Logger.LogE(err);
	}
}
private function OnKeyDown(event:KeyboardEvent):void{
	try{
		this.PerformDeleteService();
	}catch( err:Error){
		Logger.LogE(err);
	}
}
private function OnDeleteConfirmation(evt:CloseEvent):void{
	try{
		if ( evt.detail == Alert.YES ){
			dbServices.Delete(this.dgServices.selectedItem.serviceid);
			this.completeServicesData = dbServices.List();
			this.ShowFilterData();
		}
	}catch(err:Error){
		Logger.LogE(err);
	}
}
private function OnServiceID_Changed():void{
	try{
		this.ShowFilterData();
	}catch(err:Error){
		Logger.LogE(err);
	}
}

private function OnServiceDescription_Changed():void{
	try{
		this.ShowFilterData();
	}catch(err:Error){
		Logger.LogE(err);
	}
}

private function OnRate_Changed():void{
	try{
		this.ShowFilterData();
	}catch(err:Error){
		Logger.LogE(err);
	}
}

private function ShowFilterData():void{
	try{
		this.servicesData = new Array();
		
		for each ( var singleData:Object in this.completeServicesData ){
			this.servicesData.push(singleData);
		}
		
		var strValue:String = "";
		var i:Number = 0;
		var obj:Object;
		
		if( this.txtServiceID != null){
			strValue = this.txtServiceID.text;
			for( i = 0 ; i < this.servicesData.length ; i++){
				obj = this.servicesData[i];
				
				if ( String(obj.serviceid).indexOf(strValue) < 0 ){
					this.servicesData.splice(i,1);
					i --;
				}
			}
		}
		if ( this.txtServiceDescription.text != "" ){
			strValue = this.txtServiceDescription.text.toLowerCase();
			for ( i = 0 ; i < this.servicesData.length ; i++ ){
				obj = this.servicesData[i];
				
				if ( String(obj.servicename).toLowerCase().indexOf(strValue) < 0 ){
					this.servicesData.splice(i,1);
					i --;
				}
			}
		}

		if ( this.txtRates.text != "" ){
			strValue = this.txtRates.text.toLowerCase();
			for ( i = 0 ; i < this.servicesData.length ; i++ ){
				obj = this.servicesData[i];
				
				if ( String(obj.completeprice).toLowerCase().indexOf(strValue) < 0 ){
					this.servicesData.splice(i,1);
					i --;
				}
			}
		}
	}catch(err:Error){
		Logger.LogE(err);
	}
}

private function OnResetFilters_Clicked():void
{
	try{
		this.txtServiceID.text = "";
		this.txtServiceDescription.text = "";
		this.txtRates.text = "";
	}catch(err:Error){
		Logger.LogE(err);
	}
}

private function OnAddNewService():void{
	try{
		var newService:twSaveService = PopUpManager.createPopUp(this,twSaveService,true) as twSaveService;
		newService.addEventListener(FlexEvent.REMOVE,OnCloseService);
		PopUpManager.centerPopUp(newService);
	}catch(err:Error){
		Logger.LogE(err);
	}
}
private function RefreshData():void{
	try{
		this.completeServicesData = dbServices.List();
		this.dgServices.dataProvider = this.completeServicesData;
	}catch(err:Error){
		Logger.LogE(err);
	}
}
private function OnCloseService(evt:FlexEvent):void{
	try{
		this.RefreshData();
		this.ShowFilterData();
	}catch(err:Error){
		Logger.LogE(err);
	}
}